// Motion Editor (position and radius of disk interpolating timed keyframes)

CLOUD C = new CLOUD(2); // keyframes edited by user
int k=0;  // current keys
float f=0; // frame
float df=0.01; // frame increment
Boolean animating=true, useM=false, threeD=false, interpolating = false; //if interpolating, draw the inbetween frames
int timeVar = 4; //produces more points along the curve when increased, making the dancer move at a slower rate

void MYsetup() {
  C.declarePoints(); 
  C.resetPoints();
  song.play();
}

void MYmenu () {
  scribeFooter("p:play animation, f:film animation, t:change time (drag mouse), 3:3D, 2:2D", 2);
  scribeFooter("m:move, r:rotate, z:zoom, _:append, a:angle, c:weightChange, x:delete, s:save, l:load, =:reset", 1);
}

void MYdraw () {
  if (threeD) {
    directionalLight(250, 250, 250, 20, 100, -100);
    pushMatrix();
    translate(width/2, height/2, height/2-mouseY); 
    rotateX(70*PI/180);
    rotateZ(PI*(mouseX-300)/400);
    translate(-width/2, -height/2, 0);
    translate(C.P[0].x/2-curx/2,C.P[0].y/2-cury/2,0); //camera tracking, move the camera with the dancer
    
    

    //        translate(0.0, 9.0, (300-mouseY)/4);
    //        rotateX(75*PI/180);

    C.drawKeys3D(); 
    C.drawPickedKeys3D();
    if(animating){ //check for animation, toggled by the 'p' key
      if(animCount > keyframes.size()-1){ //if we've reached the end of the animation, start over
        animCount=0;
      }
      velocity = 1; //velocity 1 allows for animation
      animation(threeD);
    } else {
      velocity = 0; //velocity 0 pauses animation
      animation(threeD);
    }
    popMatrix();
  }
  else {
    if(interpolating){  //if key 'i' has been toggled, draw the inbetween frames
      drawFrames();
    }
    stroke(orange); 
    noFill(); 
    C.drawKeys(); //draw the 3D keys that arent selected in orange
    C.drawPickedKeys(); //draw the selected, previous and next in different colors
    fill(black); 
    C.writePointIDs(); 
    noFill(); 
    //  showFrame(f);
    if(animating){
      if(animCount > keyframes.size()-1){
        animCount=0;
      }
      velocity = 1;
      animation(threeD);
    } else {
      velocity = 0;
      animation(threeD);
    }
  }
  if (capturing) captureFrame(); // when filming and '.' was pressed, we want to capture all animation frames into the film
  if (animating) { 
    f+=df;  
    if (f>1) {
      f=0; 
      capturing=false;
    }
    
  } // advance time if animating and stop capturing when reached the end
}

void MYmouseMoved() {
  
}  // executed when the mouse is moved (not dragged): mouse button is not pressed but the location changed

void MYmousePressed() {  // executed when the mouse is pressed
  if (!Mouse().isInWindow()) return; // do nothing if pressed out of the window (can change that method of pt to restric where one can click
  if (!keyPressed || key=='a' )  C.pickClosestPoint(Mouse()); // used to pick the closest vertex of C to the mouse
  else {
    if (key==' ') C.appendPoint(Mouse()); // insert a new vertex in C
    if (key=='x') { 
      C.pickClosestPoint(Mouse()); 
      C.deletePoint();
    } // delete the closest vertex
    if (key=='c') { 
      C.pickClosestPoint(Mouse()); 
      C.weightChange();
    }
  }
  calcFrames(); //calculate the inbetweens
}

void MYmouseDragged() {
  calcFrames();
  if (!keyPressed) {
    C.dragPoint();
  } // drag selected point with mouse
  if (keyPressed) {
    if (key=='t') f+=float(mouseX-pmouseX)/width;  // adjust current frame   
    if (key=='m') C.translatePoints(MouseDrag()); // rotate all points around their center of mass
    if (key=='r') C.rotatePointsAroundCenterOfMass(Mouse(), Pmouse()); // rotate all points around their center of mass
    if (key=='a') C.rotateOrientation(); // rotate all points around their center of mass
    if (key=='z') C.scalePointsAroundCenterOfMass(Mouse(), MouseDrag()); // scale all points with respect to their center of mass
  }
}  

void MYmouseReleased() {
  
}  

void MYkeyPressed() {
  if (key=='p') {
    animating=!animating; 
    f=0;
  }   // toggles playing animation
  if (key=='f') {
    capturing=true; 
    animating=true; 
    f=0;
  }
  if (key=='l') {
    C.loadPts();
  }
  if (key=='=') {
    C.resetPoints();
  }
  if (key=='s') C.savePts(); 
  if (key=='3') { //switch to 3D when press 3
    timeVar = 1; //allows 3D to run fasted since drawing takes longer
    curDivision = 29*timeVar;
    threeD=true;
  }
  if (key=='2') {
    timeVar=4;
    curDivision = 29*timeVar;
    threeD=false;
  }
  if (key=='i') interpolating=!interpolating;
  calcFrames();
}  

void  MYkeyReleased() {
}

